from pyecharts import options as opts
from pyecharts.charts import Line, Timeline
import pandas as pd

from get_data import get_data

# 读取数据
df = get_data()

# 获取所有年份
years = sorted(df['year'].unique())

# 创建时间线对象
timeline = Timeline(init_opts=opts.InitOpts(width="1200px", height="750px"))

# 为每一年创建折线图
for year in years:
    # 获取该年份的数据
    year_data = df[df['year'] == year].copy()

    # 计算每个厂商的总销量并排序
    manufacturer_sales = year_data.groupby('manufacturer')['sales_volume'].sum().reset_index()
    top10_manufacturers = manufacturer_sales.sort_values('sales_volume', ascending=False).head(10)[
        'manufacturer'].tolist()

    # 筛选出Top10厂商的数据
    top10_data = year_data[year_data['manufacturer'].isin(top10_manufacturers)]

    # 创建折线图
    line = Line()

    # 设置横轴为月份
    months = sorted(top10_data['month'].unique())
    line.add_xaxis([str(month) for month in months])

    # 添加每个厂商的数据（移除颜色设置）
    for idx, manufacturer in enumerate(top10_manufacturers):
        # 获取该厂商在各月份的销量
        manufacturer_monthly_data = top10_data[top10_data['manufacturer'] == manufacturer].sort_values('month')
        sales_by_month = manufacturer_monthly_data['sales_volume'].tolist()

        line.add_yaxis(
            series_name=manufacturer,
            y_axis=sales_by_month,
            symbol_size=8,
            label_opts=opts.LabelOpts(is_show=False, position="top"),
            # 移除 itemstyle_opts 颜色设置
            linestyle_opts=opts.LineStyleOpts(width=2.5)
        )

    # 设置全局选项
    line.set_global_opts(
        title_opts=opts.TitleOpts(
            title=f"{year}年汽车厂商销量TOP10 - 月度趋势",
            pos_left="center",
            pos_top="3px",
            title_textstyle_opts=opts.TextStyleOpts(font_size=20)
        ),
        tooltip_opts=opts.TooltipOpts(
            trigger="axis",
            axis_pointer_type="cross"
        ),
        yaxis_opts=opts.AxisOpts(
            name="销量(辆)",
            type_="value",
            min_=0,
            splitline_opts=opts.SplitLineOpts(
                is_show=True,
                linestyle_opts=opts.LineStyleOpts(
                    width=2,
                    type_="dashed",
                    color="#e0e0e0"
                )
            )
        ),
        xaxis_opts=opts.AxisOpts(
            name="月份",
            type_="category",
            boundary_gap=False,
            splitline_opts=opts.SplitLineOpts(
                is_show=True,
                linestyle_opts=opts.LineStyleOpts(
                    width=2,
                    type_="dashed",
                    color="#e0e0e0"
                )
            )
        ),
        legend_opts=opts.LegendOpts(
            type_="scroll",
            pos_top="50px",
            orient="vertical",
            pos_right="10px",
            item_width=12,
            item_height=12,
            textstyle_opts=opts.TextStyleOpts(font_size=12)
        )
    )

    # 添加到时间线
    timeline.add(line, str(year))

# 设置时间线选项（居中显示）
timeline.add_schema(
    is_auto_play=False,
    play_interval=3000,
    orient="horizontal",
    pos_left="20%",    # 左边距20%
    pos_right="20%",   # 右边距20%
    pos_bottom="0px",  # 底部位置
    width="60%",       # 宽度60%
    label_opts=opts.LabelOpts(font_size=12, color="#333")
)

# 生成HTML文件
timeline.render("monthly_top10_manufacturers_line.html")